<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/3/30
 * Time: 15:42
 */

namespace api\storage\model;


use think\Model;

class ClientVersionModel extends Model
{
    protected $table = 'iwh_client_version';
    protected $pk = 'id';

    public function findClientVersion($id){
        return $this->where('id',$id)->find();
    }

    /**
     * 根据版本号查询版本信息
     *
     * @param $majorVersion
     * @param int $minorVersion
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryByVersion($type,$majorVersion,$minorVersion = 0){

//        $where['type'] = $type;
//
//        $where1['major_version'] = ['gt', $majorVersion];
//
//        $where2 = [
//            'major_version' => $majorVersion,
//            'minor_version' => ['gt', $minorVersion]
//        ];
//        return $this->where($where)
//            ->where($where1)
//            ->whereOr($where2)
//            ->order('major_version desc,minor_version desc')
//            ->select(false);
//        $table = $this->table;
//        iwh_client_version
        $result =  $this->query("select * from {$this->table} where type = ? and (major_version > ? or (major_version = ? and minor_version > ? ))", [
            $type, $majorVersion, $majorVersion, $minorVersion
        ]);
        return $result;
    }

    /**
     * 查询最新的版本
     *
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryLatestVersion($type){
        return $this->where('type',$type)
            ->where('is_newest',1)
            ->order('id desc')
            ->select();
    }

    /**
     * 获取历史版本
     *
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryHistoryVersion($type){
        return $this->where('type',$type)
            ->order("id desc")
            ->select();
    }

}